package com.saikei.demoapp.titlebar.widget

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.graphics.Color
import android.graphics.Typeface
import android.os.Build
import android.text.InputFilter
import android.text.TextUtils
import android.util.AttributeSet
import android.util.Log
import android.util.TypedValue
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.saikei.demoapp.R
import com.saikei.demoapp.titlebar.statusbar.StatusBarUtils
import com.saikei.demoapp.titlebar.utils.ScreenUtils

/**
 * 通用标题栏
 *
 *
 * <declare-styleable name="GCommonTitleBar">
 * <attr name="titleBarBg" format="reference"></attr>
 * <attr name="titleBarHeight" format="dimension"></attr>
 * <attr name="showBottomLine" format="boolean"></attr>
 * <attr name="bottomLineColor" format="color"></attr>
 * <attr name="bottomShadowHeight" format="dimension"></attr>
 *
 * <attr name="statusBarMode" format="enum">
 * <enum name="dark" value="0"></enum>
 * <enum name="light" value="1"></enum>
</attr> *
 *
 *
 *
 * <attr name="leftType">
 * <enum name="none" value="0"></enum>
 * <enum name="textView" value="1"></enum>
 * <enum name="imageButton" value="2"></enum>
 * <enum name="customView" value="3"></enum>
</attr> *
 *
 *
 * <attr name="leftText" format="string"></attr>
 * <attr name="leftTextColor" format="color"></attr>
 * <attr name="leftTextSize" format="dimension"></attr>
 * <attr name="leftDrawable" format="reference"></attr>
 * <attr name="leftDrawablePadding" format="dimension"></attr>
 * <attr name="leftImageResource" format="reference"></attr>
 * <attr name="leftCustomView" format="reference"></attr>
 *
 *
 *
 * <attr name="rightType">
 * <enum name="none" value="0"></enum>
 * <enum name="textView" value="1"></enum>
 * <enum name="imageButton" value="2"></enum>
 * <enum name="customView" value="3"></enum>
</attr> *
 *
 *
 * <attr name="rightText" format="string"></attr>
 * <attr name="rightTextColor" format="color"></attr>
 * <attr name="rightTextSize" format="dimension"></attr>
 * <attr name="rightImageResource" format="reference"></attr>
 * <attr name="rightCustomView" format="reference"></attr>
 *
 *
 *
 * <enum name="none" value="0"></enum>
 * <enum name="textView" value="1"></enum>
 * <enum name="searchView" value="2"></enum>
 * <enum name="customView" value="3"></enum>
 *
 *
 *
 * <attr name="centerText" format="string"></attr>
 * <attr name="centerTextColor" format="color"></attr>
 * <attr name="centerTextSize" format="dimension"></attr>
 * <attr name="centerTextMarquee" format="boolean"></attr>
 * <attr name="centerSubText" format="string"></attr>
 * <attr name="centerSubTextColor" format="color"></attr>
 * <attr name="centerSubTextSize" format="dimension"></attr>
 * <attr name="centerSearchEdiable" format="boolean"></attr>
 * <attr name="centerCustomView" format="reference"></attr>
</declare-styleable> *
 *
 *
 * Created by wuhenzhizao on 16/1/12.
 */
class CommonTitleBar : RelativeLayout, View.OnClickListener {

    /**
     * 获取标题栏底部横线
     * @return
     */
    private var bottomLine: View? = null // 分隔线视图
    private var rlMain: RelativeLayout? = null // 主视图
    private var resDefault: Int = -1 // 主视图

    /**
     * 获取标题栏左边TextView，对应leftType = textView
     * @return
     */
    private var leftTextView: TextView? = null // 左边TextView

    /**
     * 获取标题栏左边ImageButton，对应leftType = imageButton
     * @return
     */
    private var leftImageButton: ImageButton? = null // 左边ImageButton

    /**
     * 获取左边自定义布局
     * @return
     */
    private var leftCustomView: View? = null

    /**
     * 获取标题栏右边TextView，对应rightType = textView
     * @return
     */
    private var rightTextView: TextView? = null // 右边TextView

    /**
     * 获取标题栏右边ImageButton，对应rightType = imageButton
     * @return
     */
    private var rightImageButton: ImageButton? = null // 右边ImageButton

    /**
     * 获取右边自定义布局
     * @return
     */
    private var rightCustomView: View? = null

    private var centerLayout: LinearLayout? = null

    /**
     * 获取标题栏中间TextView，对应centerType = textView
     * @return
     */
    private var centerTextView: TextView? = null // 标题栏文字
    private var centerSubTextView: TextView? = null // 副标题栏文字

    /**
     * 获取中间自定义布局视图
     * @return
     */
    private var centerCustomView: View? = null // 中间自定义视图

    private var titleBarColor = Color.BLUE // 标题栏背景颜色
    private var titleBarHeight = 80// 标题栏高度
    private var statusBarColor = Color.BLUE // 状态栏颜色

    private var showBottomLine = false // 是否显示底部分割线
    private var bottomLineColor = Color.BLACK // 分割线颜色
    private var bottomShadowHeight = 1f // 底部阴影高度

    private var leftText: String? = null // 左边TextView文字
    private var leftTextBold: Boolean = true // 左边TextView文字
    private var leftTextMaxLength: Int = 8 // 左边TextView文字
    private var leftTextColor = Color.BLACK // 左边TextView颜色
    private var leftTextSize = 16f // 左边TextView文字大小

    @SuppressLint("UseCompatLoadingForDrawables")
    private var leftDrawable = R.drawable.comm_back_black // 左边TextView drawableLeft资源
    private var leftDrawablePadding = 0f // 左边TextView drawablePadding
    private var leftImageResource = resDefault // 左边图片资源
    private var leftCustomViewRes = resDefault // 左边自定义视图布局资源

    private var rightText: String? = null // 右边TextView文字
    private var rightTextBold: Boolean = true // 左边TextView文字
    private var rightTextMaxLength: Int = 8 // 左边TextView文字
    private var rightTextColor = Color.BLACK // 右边TextView颜色
    private var rightTextSize = 16f // 右边TextView文字大小
    private var rightImageResource = resDefault // 右边图片资源
    private var rightCustomViewRes = resDefault // 右边自定义视图布局资源

    private var centerText: String? = null // 中间TextView文字
    private var centerTextBold: Boolean = true // 左边TextView文字
    private var centerTextMaxLength: Int = 8 // 左边TextView文字
    private var centerTextColor = Color.BLACK // 中间TextView字体颜色
    private var centerTextSize = 18f // 中间TextView字体大小
    private var centerTextMarquee = false // 中间TextView字体是否显示跑马灯效果
    private var centerSubText: String? = null // 中间subTextView文字
    private var centerSubTextMaxLength: Int = 8 // 左边TextView文字
    private var centerSubTextColor = Color.BLACK // 中间subTextView字体颜色
    private var centerSubTextSize = 16f // 中间subTextView字体大小
    private var centerCustomViewRes = resDefault // 中间自定义布局资源
    private var PADDING_12 = 0
    private var margin = 120f
    private var listener: OnTitleBarListener? = null
    private var doubleClickListener: OnTitleBarDoubleClickListener? = null

    constructor(context: Context, attrs: AttributeSet) : super(
        context, attrs
    ) {
        loadAttributes(context, attrs)
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        initGlobalViews()
        initMainViews()
    }

    private fun loadAttributes(context: Context, attrs: AttributeSet) {
        PADDING_12 = ScreenUtils.dp2PxInt(context, 12f)
        val array = context.obtainStyledAttributes(attrs, R.styleable.CommonTitleBar)
        titleBarColor = array.getColor(R.styleable.CommonTitleBar_titleBarColor, titleBarColor)
        showBottomLine = array.getBoolean(R.styleable.CommonTitleBar_showBottomLine, true)
        bottomLineColor =
            array.getColor(R.styleable.CommonTitleBar_bottomLineColor, Color.parseColor("#dddddd"))
        bottomShadowHeight = array.getDimension(
            R.styleable.CommonTitleBar_bottomShadowHeight,
            ScreenUtils.dp2PxInt(context, 0f).toFloat()
        )

        leftText = array.getString(R.styleable.CommonTitleBar_leftText)
        leftText?.takeIf { it.isNotEmpty() }?.also {
            leftTextBold = array.getBoolean(R.styleable.CommonTitleBar_leftTextBold, leftTextBold)
            leftTextColor = array.getColor(R.styleable.CommonTitleBar_leftTextColor, leftTextColor)
            leftTextMaxLength =
                array.getInt(R.styleable.CommonTitleBar_leftTextMaxLength, leftTextMaxLength)
            leftTextSize = array.getDimension(
                R.styleable.CommonTitleBar_leftTextSize,
                ScreenUtils.dp2PxInt(context, leftTextSize).toFloat()
            )
            leftDrawable = array.getResourceId(R.styleable.CommonTitleBar_leftDrawable, resDefault)
            leftDrawablePadding =
                array.getDimension(R.styleable.CommonTitleBar_leftDrawablePadding, 5f)
        } ?: array.getResourceId(R.styleable.CommonTitleBar_leftImageResource, resDefault)
            .takeIf { it != resDefault }?.also { leftImageResource = it }
        ?: array.getResourceId(R.styleable.CommonTitleBar_leftCustomView, resDefault)
            .takeIf { it != resDefault }?.also { leftCustomViewRes = it }


        rightText = array.getString(R.styleable.CommonTitleBar_rightText)
        rightText?.takeIf { it.isNotEmpty() }?.also {
            rightTextBold =
                array.getBoolean(R.styleable.CommonTitleBar_rightTextBold, rightTextBold)
            rightTextMaxLength =
                array.getInt(R.styleable.CommonTitleBar_rightTextMaxLength, rightTextMaxLength)
            rightTextColor = array.getColor(
                R.styleable.CommonTitleBar_rightTextColor, rightTextColor
            )
            rightTextSize = array.getDimension(
                R.styleable.CommonTitleBar_rightTextSize,
                ScreenUtils.dp2PxInt(context, rightTextSize).toFloat()
            )
        } ?: array.getResourceId(R.styleable.CommonTitleBar_rightImageResource, resDefault)
            .takeIf { it != resDefault }?.also { rightImageResource = it }
        ?: array.getResourceId(R.styleable.CommonTitleBar_rightCustomView, resDefault)
            .takeIf { it != resDefault }?.also { rightCustomViewRes = it }

        centerText = array.getString(R.styleable.CommonTitleBar_centerText)
        centerText?.takeIf { it.isNotEmpty() }?.also {
            centerTextBold =
                array.getBoolean(R.styleable.CommonTitleBar_centerTextBold, centerTextBold)
            centerTextMaxLength =
                array.getInt(R.styleable.CommonTitleBar_centerTextMaxLength, centerTextMaxLength)
            centerTextColor = array.getColor(
                R.styleable.CommonTitleBar_centerTextColor, centerTextColor
            )
            centerTextSize = array.getDimension(
                R.styleable.CommonTitleBar_centerTextSize,
                ScreenUtils.dp2PxInt(context, centerTextSize).toFloat()
            )
            centerTextMarquee = array.getBoolean(R.styleable.CommonTitleBar_centerTextMarquee, true)
            centerSubText = array.getString(R.styleable.CommonTitleBar_centerSubText)
            centerSubText?.takeIf { it.isNotEmpty() }?.also {
                centerSubTextMaxLength = array.getInt(
                    R.styleable.CommonTitleBar_centerSubTextMaxLength, centerSubTextMaxLength
                )
                centerSubTextColor = array.getColor(
                    R.styleable.CommonTitleBar_centerSubTextColor, centerSubTextColor
                )
                centerSubTextSize = array.getDimension(
                    R.styleable.CommonTitleBar_centerSubTextSize,
                    ScreenUtils.dp2PxInt(context, centerSubTextSize).toFloat()
                )
            }
        } ?: array.getResourceId(R.styleable.CommonTitleBar_centerCustomView, resDefault)
            .takeIf { it != resDefault }?.also { centerCustomViewRes = it }

        array.recycle()
    }

    private val MATCH_PARENT by lazy {
        ViewGroup.LayoutParams.MATCH_PARENT
    }
    private val WRAP_CONTENT by lazy {
        ViewGroup.LayoutParams.WRAP_CONTENT
    }

    /**
     * 初始化全局视图
     *
     * @param context       上下文
     */
    private fun initGlobalViews() {
        layoutParams = ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)
        rlMain = RelativeLayout(context)
        rlMain?.id = StatusBarUtils.generateViewId()
        rlMain?.setBackgroundColor(titleBarColor)
        setBackgroundColor(titleBarColor)
        addView(rlMain)
        // 构建分割线视图
        if (showBottomLine) {
            // 已设置显示标题栏分隔线,5.0以下机型,显示分隔线
            bottomLine = View(context)
            bottomLine?.setBackgroundColor(bottomLineColor)
            val bottomLineParams = LayoutParams(MATCH_PARENT, ScreenUtils.dp2PxInt(context, bottomShadowHeight))
            bottomLineParams.addRule(BELOW, rlMain?.id!!)
            addView(bottomLine, bottomLineParams)
        }
    }

    /**
     * 初始化主视图
     */
    private fun initMainViews() {
        initMainLeftViews(context)
        initMainRightViews(context)
        initMainCenterViews(context)
    }

    /**
     * 初始化主视图左边部分
     * -- add: adaptive RTL
     * @param context       上下文
     */
    private fun initMainLeftViews(context: Context) {
        val leftInnerParams = LayoutParams(WRAP_CONTENT, MATCH_PARENT)
        leftInnerParams.addRule(ALIGN_PARENT_START)
        leftInnerParams.addRule(CENTER_VERTICAL)
        leftText?.takeIf { it.isNotEmpty() }?.also {
            leftTextView = TextView(context).apply {
                id = StatusBarUtils.generateViewId()
                text = leftText
                typeface =
                    if (leftTextBold) Typeface.defaultFromStyle(Typeface.BOLD) else Typeface.defaultFromStyle(
                        Typeface.NORMAL
                    )
                setTextLength(leftTextMaxLength)
                setTextColor(leftTextColor)
                setTextSize(TypedValue.COMPLEX_UNIT_PX, leftTextSize)
                gravity = Gravity.START or Gravity.CENTER_VERTICAL
                isSingleLine = true
                setOnClickListener(this@CommonTitleBar)
                if (leftDrawable != resDefault) {
                    compoundDrawablePadding = leftDrawablePadding.toInt()
                    setCompoundDrawablesRelativeWithIntrinsicBounds(leftDrawable, 0, 0, 0)
                }
                setPadding(PADDING_12, 0, PADDING_12, 0)
            }.also { view ->
                rlMain?.addView(view, leftInnerParams)
            }
        } ?: leftImageResource.takeIf { it != resDefault }?.let {
            ImageButton(context).apply {
                id = StatusBarUtils.generateViewId()
                setBackgroundColor(Color.TRANSPARENT)
                setImageResource(it)
                setPadding(PADDING_12, 0, PADDING_12, 0)
                setOnClickListener(this@CommonTitleBar)
            }.also { view ->
                leftImageButton = view
                rlMain?.addView(view, leftInnerParams)
            }
        } ?: leftCustomViewRes.takeIf { it != resDefault }?.let {
            LayoutInflater.from(context).inflate(it, rlMain, false).apply {
                if (id == NO_ID) id = StatusBarUtils.generateViewId()
            }.also { view ->
                leftCustomView = view
                rlMain?.addView(view, leftInnerParams)
            }
        }

    }

    /**
     * 初始化主视图右边部分
     * -- add: adaptive RTL
     * @param context       上下文
     */
    private fun initMainRightViews(context: Context) {
        val rightInnerParams = LayoutParams(WRAP_CONTENT, MATCH_PARENT)
        rightInnerParams.addRule(ALIGN_PARENT_END)
        rightInnerParams.addRule(CENTER_VERTICAL)
        rightText?.takeIf { it.isNotEmpty() }?.also {
            rightTextView = TextView(context).apply {
                id = StatusBarUtils.generateViewId()
                text = rightText
                typeface =
                    if (rightTextBold) Typeface.defaultFromStyle(Typeface.BOLD) else Typeface.defaultFromStyle(
                        Typeface.NORMAL
                    )
                setTextLength(rightTextMaxLength)
                setTextColor(rightTextColor)
                setTextSize(TypedValue.COMPLEX_UNIT_PX, rightTextSize)
                gravity = Gravity.END or Gravity.CENTER_VERTICAL
                isSingleLine = true
                setPadding(PADDING_12, 0, PADDING_12, 0)
                setOnClickListener(this@CommonTitleBar)
            }.also {
                rlMain?.addView(it, rightInnerParams)
            }
        } ?: rightImageResource.takeIf { it != resDefault }?.let {
            ImageButton(context).apply {
                id = StatusBarUtils.generateViewId()
                setImageResource(it)
                setBackgroundColor(Color.TRANSPARENT)
                scaleType = ImageView.ScaleType.CENTER_INSIDE
                setPadding(PADDING_12, 0, PADDING_12, 0)
                setOnClickListener(this@CommonTitleBar)
            }.also { view ->
                rightImageButton = view
                rlMain?.addView(view, rightInnerParams)
            }
        } ?: rightCustomViewRes.takeIf { it != resDefault }?.let {
            LayoutInflater.from(context).inflate(it, rlMain, false).apply {
                if (id == NO_ID) id = StatusBarUtils.generateViewId()
            }.also { view ->
                rightCustomView = view
                rlMain?.addView(view, rightInnerParams)
            }
        }
    }

    /**
     * 初始化主视图中间部分
     * @param context   上下文
     */
    private fun initMainCenterViews(context: Context) {
        centerText?.takeIf { it.isNotEmpty() }?.let { tx ->
            // 创建容器布局
            centerLayout = LinearLayout(context).apply {
                id = StatusBarUtils.generateViewId()
                gravity = Gravity.CENTER
                orientation = LinearLayout.VERTICAL
                setOnClickListener(this@CommonTitleBar)
            }.also { view ->
                rlMain?.addView(view, LayoutParams(WRAP_CONTENT, MATCH_PARENT).apply {
                    marginStart = ScreenUtils.dp2PxInt(context, margin)
                    marginEnd = ScreenUtils.dp2PxInt(context, margin)
                    addRule(CENTER_IN_PARENT)
                })
            }

            // 添加主标题
            centerTextView = TextView(context).apply {
                text = tx
                typeface =
                    if (centerTextBold) Typeface.defaultFromStyle(Typeface.BOLD) else Typeface.defaultFromStyle(
                        Typeface.NORMAL
                    )
                setTextLength(centerTextMaxLength)
                setTextColor(centerTextColor)
                setTextSize(TypedValue.COMPLEX_UNIT_PX, centerTextSize)
                gravity = Gravity.CENTER
                isSingleLine = true
                if (centerTextMarquee) {
                    ellipsize = TextUtils.TruncateAt.MARQUEE
                    marqueeRepeatLimit = -1
                    isSelected = true
                    requestFocus()
                }
            }.also { view ->
                centerLayout?.addView(
                    view, LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT)
                )
            }

            // 添加副标题
            centerSubTextView = TextView(context).apply {
                text = centerSubText
                setTextLength(centerSubTextMaxLength)
                setTextColor(centerSubTextColor)
                setTextSize(TypedValue.COMPLEX_UNIT_PX, centerSubTextSize)
                gravity = Gravity.CENTER
                isSingleLine = true
                if (TextUtils.isEmpty(centerSubText)) visibility = GONE
            }.also { view ->
                centerLayout?.addView(
                    view, LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT)
                )
            }

        } ?: centerCustomViewRes.takeIf { it != resDefault }?.let {
            LayoutInflater.from(context).inflate(it, rlMain, false).apply {
                if (id == NO_ID) id = StatusBarUtils.generateViewId()
            }.also { view ->
                centerCustomView = view
                rlMain?.addView(view, LayoutParams(WRAP_CONTENT, MATCH_PARENT).apply {
                    marginStart = PADDING_12
                    marginEnd = PADDING_12
                    addRule(CENTER_IN_PARENT)
                })
            }
        }

    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        titleBarHeight = heightMeasureSpec
    }

    private val window: Window? by lazy {
        val context = context
        val activity = if (context is Activity) {
            context
        } else {
            (context as ContextWrapper).baseContext as Activity
        }
        activity.window
    }

    private var lastClickMillis: Long = 0 // 双击事件中，上次被点击时间

    override fun onClick(v: View) {
        if (listener == null) return
        if (v == centerLayout && doubleClickListener != null) {
            val currentClickMillis = System.currentTimeMillis()
            if (currentClickMillis - lastClickMillis < 500) {
                doubleClickListener?.onClicked(v)
            }
            lastClickMillis = currentClickMillis
        } else if (v == leftTextView) {
            listener?.onClicked(v, ACTION_LEFT_TEXT, null)
        } else if (v == leftImageButton) {
            listener?.onClicked(v, ACTION_LEFT_BUTTON, null)
        } else if (v == rightTextView) {
            listener?.onClicked(v, ACTION_RIGHT_TEXT, null)
        } else if (v == rightImageButton) {
            listener?.onClicked(v, ACTION_RIGHT_BUTTON, null)
        } else if (v == centerTextView) {
            listener?.onClicked(v, ACTION_CENTER_TEXT, null)
        }
    }

    /**
     * 设置背景颜色
     * @param color
     */
    override fun setBackgroundColor(color: Int) {
        rlMain?.setBackgroundColor(color)
        super.setBackgroundColor(color)
    }

    /**
     * 设置背景图片
     *
     * @param resource
     */
    override fun setBackgroundResource(resource: Int) {
        setBackgroundColor(titleBarColor)
        super.setBackgroundResource(resource)
    }

    /**
     * 切换状态栏模式
     */
    fun checkStatusBarPadding(darkOrLight:Boolean?=null) {
        postDelayed({
            val transparentStatusBar = StatusBarUtils.supportTransparentStatusBar()
            // 构建标题栏填充视图
            if (transparentStatusBar) {
                val window = window ?: return@postDelayed
                StatusBarUtils.transparentStatusBar(window)
                // 设置 rlMain 顶部 padding
                val statusBarHeight = StatusBarUtils.getStatusBarHeight(context)
                setPadding(0, statusBarHeight, 0, 0)
                fun setAutoTextColor(){
                    val isDark = StatusBarUtils.isDark(statusBarColor)
                    if (!isDark){
                        StatusBarUtils.setDarkMode(window)
                    }else{
                        StatusBarUtils.setLightMode(window)
                    }
                }
                darkOrLight?.let {
                    if (it){
                        StatusBarUtils.setLightMode(window)
                    }else{
                        StatusBarUtils.setDarkMode(window)
                    }
                }?:setAutoTextColor()
            }
        }, 30)
    }



    /**
     * @param leftView
     */
    fun setLeftView(leftView: View) {
        if (leftView.id == NO_ID) {
            leftView.id = StatusBarUtils.generateViewId()
        }
        val leftInnerParams = LayoutParams(WRAP_CONTENT, MATCH_PARENT)
        leftInnerParams.addRule(ALIGN_PARENT_START)
        leftInnerParams.addRule(CENTER_VERTICAL)
        rlMain?.addView(leftView, leftInnerParams)
    }

    /**
     * @param centerView
     */
    fun setCenterView(centerView: View) {
        if (centerView.id == NO_ID) {
            centerView.id = StatusBarUtils.generateViewId()
        }
        val centerInnerParams = LayoutParams(WRAP_CONTENT, MATCH_PARENT)
        centerInnerParams.addRule(CENTER_IN_PARENT)
        centerInnerParams.addRule(CENTER_VERTICAL)
        rlMain?.addView(centerView, centerInnerParams)
    }

    /**
     * @param rightView
     */
    fun setRightView(rightView: View) {
        if (rightView.id == NO_ID) {
            rightView.id = StatusBarUtils.generateViewId()
        }
        val rightInnerParams = LayoutParams(WRAP_CONTENT, MATCH_PARENT)
        rightInnerParams.addRule(ALIGN_PARENT_END)
        rightInnerParams.addRule(CENTER_VERTICAL)
        rlMain?.addView(rightView, rightInnerParams)
    }

    private fun TextView.setTextLength(length: Int) {
        val maxLengthFilter = InputFilter.LengthFilter(length)
        this.filters = arrayOf(maxLengthFilter)
    }

    /**
     * 设置点击事件监听
     *
     * @param listener
     */
    fun setListener(listener: OnTitleBarListener?) {
        this.listener = listener
    }

    fun setDoubleClickListener(doubleClickListener: OnTitleBarDoubleClickListener?) {
        this.doubleClickListener = doubleClickListener
    }

    /**
     * 点击事件
     */
    interface OnTitleBarListener {
        /**
         * @param action 对应ACTION_XXX, 如ACTION_LEFT_TEXT
         */
        fun onClicked(v: View?, action: Int, extra: String?)
    }

    /**
     * 标题栏双击事件监听
     */
    interface OnTitleBarDoubleClickListener {
        fun onClicked(v: View?)
    }

    companion object {
        const val ACTION_LEFT_TEXT: Int = 1 // 左边TextView被点击
        const val ACTION_LEFT_BUTTON: Int = 2 // 左边ImageBtn被点击
        const val ACTION_RIGHT_TEXT: Int = 3 // 右边TextView被点击
        const val ACTION_RIGHT_BUTTON: Int = 4 // 右边ImageBtn被点击
        const val ACTION_CENTER_TEXT: Int = 9 // 中间文字点击
    }
}
